package characters;

import java.awt.*;
import java.awt.image.BufferedImage;

public class HitEffect {
    private Animation animation;
    private int x, y;
    private boolean hasPlayed = false;
    private boolean shouldRemove = false;
    private boolean facingRight;


    public HitEffect(String gifPath, int x, int y,boolean facingRight) {
        this.x = x;
        this.y = y;
        this.facingRight = facingRight;
        this.animation = new Animation();
        // 设置为不循环，durationMs设为动画总时长
        this.animation.addAnimation("PLAY", gifPath, false, getGifDuration(gifPath));
        this.animation.play("PLAY");

    }

    private long getGifDuration(String gifPath) {
        // 这里应该实现获取GIF总时长的方法
        // 简单实现：假设每帧100ms，共5帧
        return 500; // 实际项目应该解析GIF帧信息
    }

    public void update() {
        if (!hasPlayed) {
            animation.update();
            // 检查动画是否播放完成
            if (animation.isAnimationFinished()) {
                hasPlayed = true;
                shouldRemove = true;
            }
        }
    }

    public void draw(Graphics2D g2d) {
        if (!hasPlayed) {
            BufferedImage frame = animation.getCurrentFrame();
            if (frame != null) {
                int drawX = x - frame.getWidth() / 2;
                int drawY = y - frame.getHeight() / 2;
                if (!facingRight) {
                    g2d.drawImage(frame, drawX+60, drawY, frame.getWidth(), frame.getHeight(),null);
                } else {
                    g2d.drawImage(frame, drawX + frame.getWidth(), drawY, -frame.getWidth(), frame.getHeight(), null);
                }
            }
        }
    }

    public boolean shouldRemove() {
        return shouldRemove;
    }
}